package com.tangzilong.controller;

import com.tangzilong.common.util.PageResult;
import com.tangzilong.common.util.R;
import com.tangzilong.entity.Student;
import com.tangzilong.service.IStudentService;
import com.tangzilong.vo.StudentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {
	@Autowired
	private IStudentService studentService;
	@RequestMapping("/list")
	public R getStudent() {
		return R.ok().data("list",studentService.list());
	}
	@GetMapping("/findByPage/{page}/{pageSize}")
	public R findByPage(@PathVariable("page") Integer page,
	                    @PathVariable("pageSize") Integer pageSize) {
		PageResult<Student> pr = studentService.findByPage(page,pageSize);
		return R.ok().data("pr",pr);
	}
	@PostMapping("/search/{page}/{pageSize}")
	public R search(@PathVariable("page") Integer page,
	                @PathVariable("pageSize") Integer pageSize,
	                @RequestBody(required = false) StudentVO vo){
		PageResult<Student> pr = studentService.search(page,pageSize,vo);
		return R.ok().data("pr",pr);
	}
	@PostMapping("/save")
	public R save(@RequestBody Student student) {
		studentService.save(student);
		return R.ok();
	}
	@PostMapping("/update")
	public R update(@RequestBody Student student) {
		studentService.updateById(student);
		return R.ok();
	}
	@DeleteMapping("/delete/{id}")
	public R delete(@PathVariable String id) {
		if (studentService.getById(id) == null) {
			return R.error().message("没有此id");
		}
		studentService.removeById(id);
		return R.ok();
	}
	@DeleteMapping("/deleteBatch")
	public R deleteBatch(@RequestBody List<String> ids) {
		studentService.removeByIds(ids);
		return R.ok();
	}
}
